// Controlls the events that occur when you do an exercise. Contains methods for starting an exercise, and getting a new question
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chalmers.se.tda367.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import edu.chalmers.se.tda367.question.AdditionMathQuestion;
import edu.chalmers.se.tda367.question.DivisionMathQuestion;
import edu.chalmers.se.tda367.question.IQuestion;
import edu.chalmers.se.tda367.question.MathQuestionConFig;
import edu.chalmers.se.tda367.question.MultiplicationMathQuestion;
import edu.chalmers.se.tda367.question.SubtractionMathQuestion;
import edu.chalmers.se.tda367.gui.view.ExerciseView;

/**
 *
 * @author Hanna
 */
public class ExerciseController implements PropertyChangeListener { // ctrl ska inte implementera propertychangelistener, vyn ska göra det?

    private MathQuestionConFig mqc;
    private ExerciseView exerciseView;
    private IQuestion question;

    public IQuestion getQuestion() {
        return question;
    }

    public ExerciseController() {
        exerciseView = new ExerciseView();
        exerciseView.addPropertyChangeListener(this);
    }

    public void startExercise(MathQuestionConFig mqcf) {
        mqc = mqcf;
        nextQuestion();
    }

    public void nextQuestion() {  // checks with config should be applied, and sets the question according to it
        if (mqc.partAdd == 1) {
            question = new AdditionMathQuestion(mqc.maxXAddSub, mqc.maxYAddSub);
        } else if (mqc.partSub == 1) {
            question = new SubtractionMathQuestion(mqc.maxXAddSub, mqc.maxYAddSub);
        } else if (mqc.partMul == 1) {
            question = new MultiplicationMathQuestion(mqc.MaxXMulDiv, mqc.maxYMulDiv);
        } else if (mqc.partDiv == 1) {
            question = new DivisionMathQuestion(mqc.MaxXMulDiv, mqc.maxYMulDiv);
        }

        exerciseView.setQuestion(question.getQuestion());
    }

    public void compareAnswers(int userAnswer) { // checks if the useranswer is correct, and notifies the view
        question.saveUserAnswer(userAnswer);
        if (question.compareAnswers()) {
            exerciseView.answerIsRightOrWrong(true, 0);
        } else {
            exerciseView.answerIsRightOrWrong(false, question.getAnswer());
        }
    }

    public ExerciseView getView() {
        return exerciseView;
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("new question")) {
            nextQuestion();
        }
        if (evt.getPropertyName().equals("compare answers")) {
            compareAnswers((Integer) evt.getNewValue());
        }
    }
}
